import * as Service from '../services/userMsg';

export default {
  namespace: 'userHistory',
  state: {
    user: {
      nickname: "读取中...",
      history_msg: [],
    },
    History_msg:[]
  },
  reducers: {
    save(state, { payload: { data } }) {
      return { ...state, ...data };
    },

  },
  effects: {
    *fetch({ payload: {id} }, { call, put }) {
      const {data} = yield call(Service.UserHistory, { id });
      yield put({ type: 'save', payload: { data } });
    },
    *getmsg({ payload: { id, user_id}}, { call, put }) {
      const {data} = yield call(Service.getmsg, { id, user_id });
      yield put({ type: 'save', payload: { data } });
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        const _pathname = pathname.substring(0, 21)
        const id = pathname.substring(21)
        if (_pathname == '/UserMsg/UserHistory/') {
          dispatch({ type: 'fetch', payload: { id } });
        }
      });
    },
  },
};